import { think } from "thinkjs";
const http = require('http')


module.exports = class extends think.Service {
    async getCatList() {
        var listData = await new Promise((res, rej) => {
            let request = http.get(`http://test1.ngzb.com.cn/cms/news/get_channel/user_id/0`,
                (getList: any) => {
                    getList.setEncoding('utf8')
                    let pushData: string = ''
                    getList.on('data', (data: Object) => {
                        pushData += data
                    })
                    getList.on('end', () => {
                        res(JSON.parse(pushData))
                    })
                    request.on('error', (error: any) => {
                        rej(error)
                    })
                })
        }).then((res) => {
            return res
        }).catch(error => {
            return error
        })
        return listData
    }
    getNewsList(id: number) {

    }
    async getNewsListFirst() {
        var listData = await new Promise((res, rej) => {
            let request = http.get(`http://test1.ngzb.com.cn/cms/news/get_list/channel_id/0/user_id/0`,
                (getList: any) => {
                    getList.setEncoding('utf8')
                    let pushData: string = ''
                    getList.on('data', (data: Object) => {
                        pushData += data
                    })
                    getList.on('end', () => {
                        res(JSON.parse(pushData))
                    })
                    request.on('error', (error: any) => {
                        rej(error)
                    })
                })
        }).then((res) => {
            return res
        }).catch(error => {
            return error
        })
        return listData
    }
    async getNewsDetail(id: number) {
        var newsData = await new Promise((res, rej) => {
            let request = http.get(`http://test1.ngzb.com.cn/cms/news/get_content/news_id/${id}`,
                (getList: any) => {
                    getList.setEncoding('utf8')
                    let pushData: string = ''
                    getList.on('data', (data: Object) => {
                        pushData += data
                    })
                    getList.on('end', () => {
                        res(JSON.parse(pushData))
                    })
                    request.on('error', (error: any) => {
                        rej(error)
                    })
                })
        }).then((res) => {
            return res
        }).catch(error => {
            return error
        })
        return newsData
    }
}